#!/usr/bin/env python

#
# GLPres - 0.1
#
# The OpenGL Presentation Framework
#
# Written by: Fabio Rotondo
# OpenGL Code by: Alessandro Molina
#
# This software is Free Software under GPLv2 License
#

import sys, os
import pygame, json
from pygame.locals import *
from optparse import OptionParser

from src.gl_objects import *
from src.slide_object import *
from src.script_loader import ScriptLoader

K_SLIDE_PREV = 280
K_SLIDE_NEXT = 281
K_PRES_HIDE  = 304
K_PRES_EXIT  = 98


class GLPres ( object ):
	def __init__ ( self, script_name, debug = 0, fullscreen = 0 ):
		sl = ScriptLoader ()

		try:
			txt = sl.load ( script_name )
			script = json.loads ( txt )
		except:
			#print txt
			script = json.load ( open ( script_name ) )
			

		if debug: 
			print json.dumps( script, sort_keys=True, indent=4)

		self._slides = script [ "slides" ]
		self._world  = script [ "world" ]
		self._curr_slide = -1 
		self._fullscreen = fullscreen

		self.som = SlideObjectManager ()

	def start ( self ):
		self.world = GLWorld ( int ( self._world.get ( "width", 800 ) ), int ( self._world.get ( "height", 600 ) ), fullscreen = self._fullscreen )

	def next ( self ):
		self._curr_slide += 1
		if self._curr_slide > len ( self._slides ):
			sys.exit ( 0 )

		self._show ()

	def _show ( self ):
		slide = self._slides [ self._curr_slide ] 
		objs  = slide [ "objects" ]

		# Clear the GL Environment
		self.world.clear ()

		# Clear the in-memory Slide
		self.som.clear ()

		for obj in objs:
			#self.world.add ( self._create_obj ( obj ) )
			self.som.add ( obj )

		self._run ()

	def _run ( self ):
		self.som.init_slides ( self.world )

		clock = pygame.time.Clock ()
	
		done = False
		start_tick = pygame.time.get_ticks ()

		while not done:
			ticks = pygame.time.get_ticks ()
			sec, dec = str ( ( ticks - start_tick ) / 1000.0 ).split ( "." )
			sec = int ( sec, 10 )
			dec = int ( dec, 10 )

			wait = self.som.render ( sec, dec )

			eventlist = pygame.event.get()

			for event in eventlist:
				if event.type == QUIT \
				   or event.type == KEYDOWN and event.key in  ( K_ESCAPE, K_PRES_EXIT ):
					done = True
				elif event.type == KEYDOWN and event.key in ( K_SPACE, K_PRES_HIDE ):
					wait = True

			if wait:
				dticks = self._wait_click ()
				if dticks == -1: done = True
				start_tick += dticks

			if not done:
				clock.tick ( 50 )			
				self.world.draw ()


	def _wait_click ( self ):
		t1 = pygame.time.get_ticks ()

		done = False

		clock = pygame.time.Clock ()

		while not done:
			eventlist = pygame.event.get()

			for event in eventlist:
				if event.type == KEYUP:
					print "---- ", event.key

				if event.type == KEYDOWN and event.key in ( K_SPACE, K_SLIDE_NEXT ):
					done = True
				elif event.type == MOUSEBUTTONDOWN:
					done = True
				elif event.type == QUIT:
					return -1
				elif event.type == KEYDOWN and event.key in ( K_PRES_EXIT, ):
					return -1

			clock.tick ( 50 )

		t2 = pygame.time.get_ticks ()

		return t2-t1

		
		

			


def main ():
	parser = OptionParser ()

	parser.add_option ( "-s", "--script", dest = "script", help = "Script to be shown" )
	parser.add_option ( "-f", "--fullscreen", dest = "fullscreen", action="store_true", help = "Run in fullscreen mode", default = False )

	( options, args ) = parser.parse_args ()

	pres = GLPres ( options.script, debug = 1, fullscreen = options.fullscreen )

	pygame.init()

	pres.start ()
	pres.next ()

	sys.exit ( 0 )

if __name__ == '__main__':
	main()

